using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Media;
using SDAT;
using System.Windows.Media;

namespace MKDS_Course_Editor
{
    public unsafe partial class swav : Form
    {
        int pause = 0;
        System.Windows.Media.MediaPlayer m = new System.Windows.Media.MediaPlayer();
        byte[] file_;
        string filename_;
        byte[] b;
        public swav(string file)
        {
            file_ = File.ReadAllBytes(file);
            filename_ = Path.GetFileName(file);
            InitializeComponent();
            m.MediaEnded += new EventHandler(m_MediaEnded);
        }
        public swav(byte[] file, string filename)
        {
            file_ = file;
            filename_ = filename;
            InitializeComponent();
            m.MediaEnded += new EventHandler(m_MediaEnded);
        }

        void m_MediaEnded(object sender, EventArgs e)
        {
            m.Stop();
            button1.Image = Properties.Resources.control;
            this.Text = "SWAV Player - [" + filename_ + "] - Stopped";
            pause = 1;
        }
        private void swav_Load(object sender, EventArgs e)
        {
            this.Text = "SWAV Player - [" + filename_ + "] - Stopped";
            SWAV.ArchivoSWAV s = SWAV.LeerArchivo(file_);
            WAV.ArchivoWAV w = SWAV.ConvertirAWAV(s);
            b = WAV.EscribirArchivo(w);
            File.WriteAllBytes(Application.ExecutablePath.Replace(Path.GetFileName(Application.ExecutablePath),"")+"tmp.wav", b);
            m.Open(new Uri(Application.ExecutablePath.Replace(Path.GetFileName(Application.ExecutablePath), "") + "tmp.wav"));
            m.Play();
            this.Text = "SWAV Player - [" + filename_ + "] - Playing";
            test:
            if (m.NaturalDuration.HasTimeSpan == true)
            {
                trackBar1.Maximum = (int)m.NaturalDuration.TimeSpan.TotalMilliseconds;
            }
            else
            {
                goto test;
            }
            button1.Image = Properties.Resources.control_pause;
            timer1.Enabled = true;
            timer1.Start();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            m.Position = TimeSpan.FromMilliseconds(trackBar1.Value);
            label2.Text = m.Position.Minutes.ToString("00") + ":" + m.Position.Seconds.ToString("00");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                trackBar1.Value = (int)m.Position.TotalMilliseconds;
            }
            catch { }
            label2.Text = m.Position.Minutes.ToString("00") + ":" + m.Position.Seconds.ToString("00");
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (pause == 0)
            {
                m.Pause();
                button1.Image = Properties.Resources.control;
                pause = 1;
                this.Text = "SWAV Player - [" + filename_ + "] - Paused";
            }
            else
            {
                m.Play();
                button1.Image = Properties.Resources.control_pause;
                this.Text = "SWAV Player - [" + filename_ + "] - Playing";
                pause = 0;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            m.Stop();
            button1.Image = Properties.Resources.control;
            this.Text = "SWAV Player - [" + filename_ + "] - Stopped";
            pause = 1;
        }

        private void swav_FormClosed(object sender, FormClosedEventArgs e)
        {
            m.Stop();
            m.Close();
            File.Delete(Application.ExecutablePath.Replace(Path.GetFileName(Application.ExecutablePath), "") + "tmp.wav");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = Path.GetFileNameWithoutExtension(filename_) + ".wav";
            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && saveFileDialog1.FileName.Length > 0)
            {
                File.WriteAllBytes(saveFileDialog1.FileName, b);
            }
        }
        
    }
}
